package com.example.exception.controller;

import com.example.exception.config.OtherException;
import com.example.exception.config.YyghException;
import com.example.exception.entity.Result;
import com.example.exception.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {
    @GetMapping("/index")
    @ResponseBody
    public Result index(String name) {
        int a = 1/0;
        return Result.ok();
    }

    @PostMapping("/div")
    @ResponseBody
    public Result div(@RequestBody User user) {
        try {
            int a = 1/0;
        } catch (Exception e) {
            throw new YyghException("数据不正确", 200);
        }
        return Result.ok();
    }

    @GetMapping("/error")
    public Result error() {
        return Result.fail();
    }

    @RequestMapping("/login")
    public ModelAndView login() {
        try {
            int a = 1/0;
        } catch (Exception e) {
            throw new OtherException("数据不正确", 200);
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("/login3")
    public String login3() {
        return "index";
    }

    @GetMapping("/login2")
    public ModelAndView login2() {
        return new ModelAndView("error/404");
    }
}
